package com.android.clinic.doctors.app.ui.main;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.android.clinic.doctors.app.R;
import com.android.clinic.doctors.app.base.BaseActivity;
import com.android.clinic.doctors.app.utils.AppManager;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 * Created by cheng on 2018/6/12.
 */
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    private RadioGroup mRadioGroup;

    private FragmentManager mFragmentManager;
    private Fragment mFragment;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private long lastBackTime = 0;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        setTitleBarTitle("");
        AppManager.getAppManager().addActivity(this);
        mRadioGroup = findViewById(R.id.radio_group);
        mRadioGroup.setOnCheckedChangeListener(this);
        initFragment();
    }

    private void initFragment() {
        mFragmentManager = getSupportFragmentManager();
        mFragment = new Fragment();

        mFragmentList.clear();
        mFragmentList.add(new HomeFragment());
        mFragmentList.add(new NewsFragment());
        mFragmentList.add(new MineFragment());

        ((RadioButton) mRadioGroup.getChildAt(0)).setChecked(true);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        String tag = mRadioGroup.findViewById(checkedId).getTag().toString();
        switchFragmentStack(Integer.parseInt(tag));
        switch (tag) {
            case "0":
            case "2":
                mImmersionBar.statusBarColorTransform(R.color.colorPrimaryDark)
                        .barAlpha(0f)
                        .init();
                break;
            case "1":
                mImmersionBar.statusBarDarkFont(true, 0.2f).init();
                break;
        }
    }

    /**
     * 获取当前Fragment
     *
     * @return
     */
    public Fragment getVisibleFragment() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        List<Fragment> fragments = fragmentManager.getFragments();
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
        return null;
    }

    private void switchFragmentStack(int index) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        Fragment fragment = mFragmentList.get(index);
        if (mFragment != fragment) {
            if (!fragment.isAdded()) {
                ft.hide(mFragment).add(R.id.container, fragment,
                        fragment.getClass().getName());
            } else {
                ft.hide(mFragment).show(fragment);
            }
            mFragment = fragment;
        }
        ft.commit();
    }

    @Override
    public void onBackPressed() {
        long currentBackTime = System.currentTimeMillis();
        if (currentBackTime - lastBackTime > 2000) {
            Toast.makeText(this, "再按一次返回键退出", Toast.LENGTH_SHORT).show();
            lastBackTime = currentBackTime;
        } else {
            super.onBackPressed();
            AppManager.getAppManager().finishAllActivity();
            System.exit(0);
        }
    }
}
